package com.yuwei.module_repair.viewmodel

import android.util.Log
import androidx.lifecycle.MutableLiveData
import com.bw.lib_common.entity.AddRepairEntity
import com.bw.lib_common.entity.RepairEntity
import com.bw.lib_common.entity.RepairEntityItem
import com.bw.lib_common.vm.BaseViewModel2
import com.yuwei.module_repair.model.RepairRepository
import kotlinx.coroutines.launch

class RepairViewModel:BaseViewModel2<RepairRepository>() {
    override fun createRepository(): RepairRepository = RepairRepository()
    val getRepairListSuccess by lazy { MutableLiveData<RepairEntity>() }
    val addRepairSuccess by lazy { MutableLiveData<RepairEntityItem>() }
    val deleteRepairSuccess by lazy { MutableLiveData<Boolean>() }
    val getRepairOrderByNumberSuccess by lazy { MutableLiveData<RepairEntityItem>() }
    val getRepairOrderByNameSuccess by lazy { MutableLiveData<RepairEntity>() }
    fun getRepairList(){
        mViewModelScope.launch(mDispatcher) {
            val result=mRepository.getRepairList()
            if (result.code==200){
                getRepairListSuccess.postValue(result.data)
            }else{
                mErrorLiveData.postValue("获取报修列表失败  ${result.message}")
            }
        }
    }
    fun addRepair(addRepairEntity: AddRepairEntity){
        mViewModelScope.launch(mDispatcher) {
            val result=mRepository.addRepair(addRepairEntity)
            Log.d("add_repair","addRepair:${result.data}")
            if (result.code==200){
                addRepairSuccess.postValue(result.data)
            }else{
                mErrorLiveData.postValue("添加报修失败 ${result.message}")
            }
        }
    }
    fun deleteRepair(id:Int){
        mViewModelScope.launch(mDispatcher) {
            val result=mRepository.deleteRepair(id)
            Log.d("delete_repair","deleteRepair:${result.data}")
            if (result.code==200){
                deleteRepairSuccess.postValue(result.data)
            }else{
                mErrorLiveData.postValue("删除维修失败 ${result.message}")
            }
        }
    }
    fun getRepairOrderByOrderNumber(orderNumber:String){
        mViewModelScope.launch(mDispatcher) {
            val result=mRepository.getRepairOrderByNumber(orderNumber)
            Log.d("get_repair_by_number","getRepairOrderByOrderNumber: ${result.data}")
            if (result.code==200){
                getRepairOrderByNumberSuccess.postValue(result.data)
            }else{
                mErrorLiveData.postValue("根据orderNumber获取失败 ${result.message}")
            }
        }
    }
    fun getRepairOrderByName(name:String){
        mViewModelScope.launch(mDispatcher) {
            val result=mRepository.getRepairOrderByName(name)
            Log.d("get_repair_by_name","getRepairOrderByName:${result.data}")
            if (result.code==200){
                getRepairOrderByNameSuccess.postValue(result.data)
            }else{
                mErrorLiveData.postValue("根据name获取失败 ${result.message}")
            }
        }
    }
}